IF EXISTS(SELECT * FROM [dbo].[sysobjects]
	WHERE ID = object_id(N'[dbo].[SPMessagesGet]') 
	AND xtype in ('P'))
	DROP PROCEDURE SPMessagesGet;
GO

CREATE PROCEDURE [dbo].[SPMessagesGet]
	@TopicId int=1,
	@MessageId int=1
AS
SELECT 
	F.ForumId
	,F.ForumName
	,F.ForumShortName
	,T.TopicId
	,T.TopicTitle
	,T.TopicShortName
	,M.MessageId
	,M.MessageBody
	,M.MessageCreationDate
	,M.MessageLastEditDate
	,M.ParentId
	,M.UserId
	,M.UserName
	,M.UserSignature
	,M.UserGroupId
	,M.UserGroupName
	,M.UserPhoto
	,M.UserRegistrationDate
	,M.Active
	
FROM 
	MessagesComplete M
		Inner join Topics T on T.TopicId = M.TopicId
		Inner join Forums F on F.ForumId = T.ForumId
WHERE 
	M.TopicId = @TopicId
	AND
	M.MessageId = @MessageId
GO

IF EXISTS(SELECT * FROM [dbo].[sysobjects]
	WHERE ID = object_id(N'[dbo].[SPMessagesUpdate]') 
	AND xtype in ('P'))
	DROP PROCEDURE SPMessagesUpdate;
GO


Create PROCEDURE [dbo].[SPMessagesUpdate]
	@TopicId int
	,@MessageId int
	,@MessageBody nvarchar(max)
	,@UserId int
	,@Ip varchar (39)
AS

	UPDATE M
	SET
		MessageBody = @MessageBody
		,MessageLastEditDate = GETUTCDATE()
		,EditIp = @Ip
		,MessageLastEditUser = @UserId
	FROM
		Messages M
	WHERE
		TopicId = @TopicId
		and MessageId = @MessageId

GO

